import React, { forwardRef } from 'react';
import * as SelectPrimitive from '@radix-ui/react-select';

// Select组件
const Select = SelectPrimitive.Root;

// SelectTrigger组件
const SelectTrigger = forwardRef<HTMLDivElement, SelectPrimitive.SelectTriggerProps>(({ className, children, ...props }, ref) => (
  <SelectPrimitive.Trigger
    ref={ref}
    className={`flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 ${className}`}
    {...props}
  >
    {children}
    <SelectPrimitive.Icon className="h-4 w-4 opacity-50" />
  </SelectPrimitive.Trigger>
));

// SelectValue组件
const SelectValue = forwardRef<HTMLSpanElement, SelectPrimitive.SelectValueProps>(({ className, ...props }, ref) => (
  <SelectPrimitive.Value
    ref={ref}
    className={`block truncate ${className}`}
    {...props}
  />
));

// SelectContent组件
const SelectContent = forwardRef<HTMLDivElement, SelectPrimitive.SelectContentProps>(({ className, children, position = 'popper', ...props }, ref) => (
  <SelectPrimitive.Portal>
    <SelectPrimitive.Content
      ref={ref}
      className={`relative z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 ${className}`}
      position={position}
      {...props}
    >
      <SelectPrimitive.Viewport className="p-1">
        {children}
      </SelectPrimitive.Viewport>
    </SelectPrimitive.Content>
  </SelectPrimitive.Portal>
));

// SelectGroup组件
const SelectGroup = forwardRef<HTMLDivElement, SelectPrimitive.SelectGroupProps>(({ className, children, ...props }, ref) => (
  <SelectPrimitive.Group
    ref={ref}
    className={`border-b ${className}`}
    {...props}
  >
    {children}
  </SelectPrimitive.Group>
));

// SelectItem组件
const SelectItem = forwardRef<HTMLOptionElement, SelectPrimitive.SelectItemProps>(({ className, children, ...props }, ref) => (
  <SelectPrimitive.Item
    ref={ref}
    className={`relative flex w-full cursor-pointer select-none items-center rounded-sm py-2 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 ${className}`}
    {...props}
  >
    <span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
      <SelectPrimitive.ItemIndicator className="h-2 w-2 fill-current" />
    </span>
    <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
  </SelectPrimitive.Item>
));

// SelectSeparator组件
const SelectSeparator = forwardRef<HTMLDivElement, SelectPrimitive.SelectSeparatorProps>(({ className, ...props }, ref) => (
  <SelectPrimitive.Separator
    ref={ref}
    className={`-mx-1 my-1 h-px bg-muted ${className}`}
    {...props}
  />
));

// 设置displayName
Select.displayName = SelectPrimitive.Root.displayName;
SelectTrigger.displayName = 'SelectTrigger';
SelectValue.displayName = 'SelectValue';
SelectContent.displayName = 'SelectContent';
SelectGroup.displayName = 'SelectGroup';
SelectItem.displayName = 'SelectItem';
SelectSeparator.displayName = 'SelectSeparator';

export { Select, SelectContent, SelectGroup, SelectItem, SelectSeparator, SelectTrigger, SelectValue };